{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7b9ef4ce-b52d-4e11-8e08-896d8dda2111",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型预测准确率: 0.98\n"
     ]
    }
   ],
   "source": [
    "# 导入鸢尾花数据集\n",
    "from sklearn.datasets import load_iris\n",
    "# 提取特征，划分数据集\n",
    "from sklearn.model_selection import train_test_split\n",
    "# 提取花瓣长度与宽度作为特征，训练模型\n",
    "x,y=load_iris().data[:,2:4],load_iris().target\n",
    "# 将数据集拆分为训练集和测试集\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=1,test_size=50)\n",
    "# 导入逻辑回归模型与评估分类准确率的方法\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "# 定义与训练逻辑回归模型\n",
    "from sklearn.metrics import accuracy_score\n",
    "# 建立逻辑回归模型\n",
    "model=LogisticRegression()\n",
    "# 训练模型\n",
    "model.fit(x_train,y_train)\n",
    "# 模型评估\n",
    "ac=accuracy_score(y_test,model.predict(x_test))\n",
    "print(\"模型预测准确率:\",ac)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d07c31a2-b9a2-4905-b08e-4fedb121b95f",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'itis_cmap' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 12\u001b[0m\n\u001b[0;32m     10\u001b[0m y_hat\u001b[38;5;241m=\u001b[39my_predict\u001b[38;5;241m.\u001b[39mreshape(x1\u001b[38;5;241m.\u001b[39mshape)\n\u001b[0;32m     11\u001b[0m iris_cmap\u001b[38;5;241m=\u001b[39mListedColormap([\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m#ACC6C0\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFF8080\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m#A0A0FF\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m---> 12\u001b[0m plt\u001b[38;5;241m.\u001b[39mpcolormesh(x1,x2,y_hat,cmap\u001b[38;5;241m=\u001b[39mitis_cmap)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'itis_cmap' is not defined"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.colors import ListedColormap\n",
    "import numpy as np\n",
    "N,M=500,500\n",
    "t1=np.linspace(0,8,N)\n",
    "t2=np.linspace(0,3,M)\n",
    "x1,x2=np.meshgrid(t1,t2)\n",
    "x_new=np.stack((x1.flat,x2.flat),axis=1)\n",
    "y_predict=model.predict(x_new)\n",
    "y_hat=y_predict.reshape(x1.shape)\n",
    "iris_cmap=ListedColormap([\"#ACC6C0\",\"FF8080\",\"#A0A0FF\"])\n",
    "plt.pcolormesh(x1,x2,y_hat,cmap=itis_cmap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57328a11-d6a0-4cbc-b0e4-eda6b755d844",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
